<?php

/**
 * 餐厅编辑
 * 
 * @copyright xinwei
 * @since 2012-04-12
 */

include_once($_SERVER["DOCUMENT_ROOT"] . "/core/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/core/admin.inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/include/foodv2/inc.php");

$g_cgival = array();
$g_pro = array();
$g_show = array();


function check_cgi_pro()
{
	global $g_cgival, $g_pro, $g_show;
	
	//关键词
	cgi::get($g_cgival["rstrt_id"], "rstrt_id", 0);
	$g_cgival["rstrt_id"] = intval($g_cgival["rstrt_id"]);
	
	//餐厅总店ID
	cgi::both($g_cgival["RstrtPID"], "RstrtPID", 0);
	$g_cgival["RstrtPID"] = intval($g_cgival["RstrtPID"]);
	
	//分店管理来的总店ID
	cgi::both($g_cgival["chain_id"], "chain_id", 0);
	$g_cgival["chain_id"] = intval($g_cgival["chain_id"]);
	
}

function get_data()
{
	global $g_cgival, $g_pro, $g_show,$config;
	
	$db = new db_web_rstrt();
	$db_cname = new db_web_rstrt_cname();
	$db_tag = new db_food_tag();
	$db_area = new db_eat_area();
	$user_db = new db_food_user();
	
	// 拿到当前站点的site_id
	$site_id = admin::admin_get_user_site_id();
	
	//获取当前site_id的城市
	$g_show["city"] = $db_area->admin_get_city_by_site_id($site_id);
	//商圈
	$areaArr = $db_tag->admin_shangjia_search($site_id, '商圈', '', -1, -1, 1, 10000);
	foreach($areaArr as $i => $val)
	{
		$g_show["area"][$val["id"]]["name"] = $val["tag_name"];
		$g_show["area"][$val["id"]]["status"] = 0;
	}
	if(!empty($g_cgival["rstrt_id"]))
	{	
		//修改
		
		// 按条件获取专辑列表
		$info = $db->admin_get_rstrt_by_ids(array($g_cgival["rstrt_id"]));
		$data['rstrt_info'] = $info[0];
		
		//显示数据处理
		if(!empty($data['rstrt_info']))
		{
			//获取别名
			$cnameArr = $db_cname->admin_get_cname_list($data['rstrt_info']['RstrtID'],1,1000);
			if(!empty($cnameArr))
			{
				foreach($cnameArr as $ii => $vv)
				{
					$cnameArr2[] = $vv['cname']; 
				}
				$data['rstrt_info']['cname'] = implode("," , $cnameArr2);
			}
			//tag
			$rstrt_tag = new db_web_rstrt_tag();
			$tag = $rstrt_tag->admin_get_tag_list($g_cgival["rstrt_id"],'','',1,1000);
			$tagArr = array();
			foreach($tag as $i => $val)
			{
				$tagArr[] = $val["tag"];
			}
			
			
			//坐标处理
			if($data['rstrt_info']['RstrtOnMapNum']!='')
			{
				$map = explode(",",$data['rstrt_info']['RstrtOnMapNum']);
				$data['rstrt_info']['map']["lat"] = substr($map[0],4);
				$data['rstrt_info']['map']["lng"] = substr($map[1],4);
			}
		}
		
		//菜系
		$styleArr = $db_tag->admin_shangjia_search($site_id, '菜系', '', -1, -1, 1, 10000);
		foreach($styleArr as $i => $val)
		{
			$g_show["style"][$val["id"]]["name"] = $val["tag_name"];
			if(in_array($val["tag_name"],$tagArr))
			{
				$g_show["style"][$val["id"]]["status"] = 1;
			}
			else
			{
				$g_show["style"][$val["id"]]["status"] = 0;
			}
		}
	
//		//风味
//		$flavorArr = $db_tag->admin_shangjia_search($site_id, '食尚', '', -1, -1, 1, 10000);
//		foreach($flavorArr as $i => $val)
//		{
//			$g_show["flavor"][$val["id"]]["name"] = $val["tag_name"];
//			if(in_array($val["tag_name"],$tagArr))
//			{
//				$g_show["flavor"][$val["id"]]["status"] = 1;
//			}
//			else
//			{
//				$g_show["flavor"][$val["id"]]["status"] = 0;
//			}
//		}
		
		//服务
		foreach($config["rstrt_service"] as $i => $val)
		{
			$g_show["rstrt_service"][$i]["name"] = $val;
			if(in_array($val,$tagArr))
			{
				$g_show["rstrt_service"][$i]["status"] = 1;
			}
			else
			{
				$g_show["rstrt_service"][$i]["status"] = 0;
			}
		}
			
		//环境
		foreach($config["rstrt_env"] as $i => $val)
		{
			$g_show["rstrt_env"][$i]["name"] = $val;
			if(in_array($val,$tagArr))
			{
				$g_show["rstrt_env"][$i]["status"] = 1;
			}
			else
			{
				$g_show["rstrt_env"][$i]["status"] = 0;
			}
		}
		
		//商圈
		//$areaArr = $db_tag->admin_shangjia_search($site_id, '商圈', '', -1, -1, 1, 10000);
		foreach($areaArr as $i => $val)
		{
			$g_show["area"][$val["id"]]["name"] = $val["tag_name"];
			if(in_array($val["tag_name"],$tagArr))
			{
				$g_show["area"][$val["id"]]["status"] = 1;
			}
			else
			{
				$g_show["area"][$val["id"]]["status"] = 0;
			}
		}
	}
	else
	{
		//新增
		
		
		//菜系
		$styleArr = $db_tag->admin_shangjia_search($site_id, '菜系', '', -1, -1, 1, 10000);
		foreach($styleArr as $i => $val)
		{
			$g_show["style"][$val["id"]]["name"] = $val["tag_name"];
			$g_show["style"][$val["id"]]["status"] = 0;
		}
	
//		//风味
//		$flavorArr = $db_tag->admin_shangjia_search($site_id, '食尚', '', -1, -1, 1, 10000);
//		foreach($flavorArr as $i => $val)
//		{
//			$g_show["flavor"][$val["id"]]["name"] = $val["tag_name"];
//			$g_show["flavor"][$val["id"]]["status"] = 0;
//		}
		
		//服务
		foreach($config["rstrt_service"] as $i => $val)
		{
			$g_show["rstrt_service"][$i]["name"] = $val;
			$g_show["rstrt_service"][$i]["status"] = 0;
		}
			
		//环境
		foreach($config["rstrt_env"] as $i => $val)
		{
			$g_show["rstrt_env"][$i]["name"] = $val;
			$g_show["rstrt_env"][$i]["status"] = 0;
		}
	}
	
	$g_show["data"] = $data;
	

	//当前参数输出到页面	
	$g_show['args'] = $g_cgival;
	$g_show['args']['city'] = $config["site_name"][$site_id]; 
}

$g_show["error"] = 0;
$g_show["errmsg"] = '';
try
{
	admin_check_login();
// 	admin_check_user_priv("foodv2/album_list.php");
	
	check_cgi_pro();
	get_data();
}
catch(Exception $e)
{
	$g_show["error"] = 1;
	$g_show["errmsg"] = $e->getMessage();
}

show_pro();
exit;


function show_pro()
{
	global $g_cgival, $g_pro, $g_show;
	if(isset($_GET['debug']))
		print_r($g_show);
	$smarty = p_get_smarty ();
	$smarty->assign ( 'g_show', $g_show );
	$smarty->display ( "foodv2/rstrt/rstrt_edit.html" );
}

?>